package com.webber.demo.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.log4j.Logger;

import com.webber.demo.common.utils.DateUtil;
import com.webber.demo.common.utils.FileUploadUtil;
import com.webber.demo.common.utils.PropertiesUtil;
import com.webber.demo.common.utils.StringUtil;
import com.webber.demo.common.utils.UploadRes;
import com.webber.demo.entity.UploadResDto;


@Controller
@RequestMapping(value="/FileUpload")
public class FileUploadController {
	protected Logger logger = Logger.getLogger(this.getClass());
	@RequestMapping(value="/{page}.page")
	public String returnPage(@PathVariable String page){
		return "jsp/"+page;
	}
	
	
	
	@RequestMapping(value="/upload",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> upload(HttpServletRequest request,HttpServletResponse response){
		Map<String, Object> data = new HashMap<String, Object>();
		List<Map<String, Object>> list = null;
		String fileName=DateUtil.getTimeStr();
		String uploadPath = null;
		try{
			uploadPath = PropertiesUtil.getValue("upload.properties", "partyConstruction.courseware.path")+"/"+fileName;
			list = FileUploadUtil.upload(request, uploadPath, false);
			
		}catch(Exception e){
			data.put("isSuccess", false);
			data.put("code", 500);
			return data;
		}
		data.put("pptsize", list.size());
		data.put("data", list);
		data.put("uploadpath", uploadPath);
		return data;
	}
	
	/**
	 * 
	 * <p>Title: uploadFile</p>
	 * <p>Description: 普通上传</p>
	 * @param request
	 * @param response
	 * @return
	 * @author JobsZhang
	 * @date 2017年10月19日 下午7:14:26
	 */
	@RequestMapping(value="/uploadFile",method=RequestMethod.POST)
	@ResponseBody
	public UploadResDto uploadFile(HttpServletRequest request,HttpServletResponse response){
		String uploadPath = null;
		UploadResDto ur=null;
		try{
			uploadPath = PropertiesUtil.getValue("upload.properties", "partyConstruction.courseware.path");
			List<UploadResDto> list = FileUploadUtil.uploadFile(request, uploadPath, false);
			if(list!=null&&!list.isEmpty()){
				ur=list.get(0);
			}
		}catch(Exception e){
			logger.debug("==========上传异常==========");
		}
		return ur;
	}
	/**
	 * 
	 * <p>Title: uploadFileChunks</p>
	 * <p>Description: 分片上传文件</p>
	 * @param request
	 * @param response
	 * @param fileMd5
	 * @param chunks
	 * @param chunk
	 * @return
	 * @author JobsZhang
	 * @date 2017年10月19日 下午5:17:22
	 */
	@RequestMapping(value="/uploadFileChunks",method=RequestMethod.POST)
	@ResponseBody
	public UploadResDto uploadFileChunks(HttpServletRequest request,HttpServletResponse response, String fileMd5,
			 String chunks, String chunk){
		logger.debug("------fileMd5------"+fileMd5);
		logger.debug("------chunks------"+chunks);
		logger.debug("------chunk------"+chunk);
		String uploadPath = null;
		UploadResDto ur=null;
		if(StringUtil.notBlank(fileMd5)&&StringUtil.notBlank(chunk)&&StringUtil.notBlank(chunks)){
			try{
				uploadPath = PropertiesUtil.getValue("upload.properties", "partyConstruction.courseware.path");
				List<UploadResDto> list = FileUploadUtil.uploadFileChunks(request, uploadPath,fileMd5,chunk, false);
				if(list!=null&&!list.isEmpty()){
					ur=list.get(0);
				}
			}catch(Exception e){
				logger.debug("==========上传异常==========");
			}
		}
		return ur;
	}
	/**
	 * 
	 * <p>Title: uploadFileChunked</p>
	 * <p>Description:检测分片是否存在 </p>
	 * @param request
	 * @param response
	 * @param fileMd5
	 * @return
	 * @author JobsZhang
	 * @date 2017年10月17日 下午5:16:58
	 */
	@RequestMapping(value="/checkChunk",method=RequestMethod.POST)
	@ResponseBody
	public UploadResDto checkChunk(HttpServletRequest request,HttpServletResponse response,@RequestParam String fileMd5,
			@RequestParam String chunk,	@RequestParam String chunkSize){
		UploadResDto ur = new UploadResDto();
		logger.debug("------fileMd5------"+fileMd5);
		String uploadPath = null;
		boolean flg = false;
		if(StringUtil.notBlank(fileMd5)){
			try{
				uploadPath = PropertiesUtil.getValue("upload.properties", "partyConstruction.courseware.path")+"/"+fileMd5+"/"+chunk;
				String path = request.getSession().getServletContext().getRealPath(uploadPath);
				File checkFile = new File(path);
				flg=checkFile.exists() && checkFile.length()==Integer.parseInt(chunkSize);
			}catch(Exception e){
				logger.debug("==========上传异常==========");
			}
		}
		ur.setCode(200);
		ur.setIfExist(flg);
		ur.setSuccess(true);
		return ur;
	}
	/**
	 * 
	 * <p>Title: fileCheckUrl</p>
	 * <p>Description:检查文件是否存在 </p>
	 * @param request
	 * @param response
	 * @param fileMd5
	 * @return
	 * @author JobsZhang
	 * @date 2017年10月17日 下午5:27:14
	 */
	@RequestMapping(value="/fileCheck",method=RequestMethod.POST)
	@ResponseBody
	public UploadResDto fileCheck(HttpServletRequest request,HttpServletResponse response,@RequestParam String fileMd5){
		logger.debug("------fileMd5------"+fileMd5);
		UploadResDto ur = new UploadResDto();
		if(StringUtil.notBlank(fileMd5)){
			UploadFileInfo fileInfo = iUploadFileInfoService.selectByMd5(fileMd5);
			logger.debug("====fileInfo:  ===="+fileInfo);
			if(fileInfo!=null){
				ur.setIfExist(true);
				ur.setCreateTime(fileInfo.getUploadTime());
				ur.setFielName(ur.getFielName());
				ur.setFileSize(fileInfo.getFileSize());
				ur.setFileType(fileInfo.getFileType());
				ur.setMediaTime(fileInfo.getFileMediaTime());
				ur.setUploadpath(fileInfo.getFileUrl());
			}
		}
		ur.setCode(200);
		ur.setSuccess(true);
		return ur;
	}
	/**
	 * 
	 * <p>Title: mergeChunks</p>
	 * <p>Description: 合并文件</p>
	 * @param request
	 * @param response
	 * @param chunks
	 * @param chunk
	 * @param fileMd5
	 * @return
	 * @author JobsZhang
	 * @date 2017年10月17日 下午5:25:49
	 */
	@RequestMapping(value="/mergeChunks",method=RequestMethod.POST)
	@ResponseBody
	public UploadResDto mergeChunks(HttpServletRequest request,HttpServletResponse response,@RequestParam String fileMd5,@RequestParam String fileName){
		logger.debug("------fileMd5------"+fileMd5);
		String uploadPath = null;
		UploadResDto ur=null;
		if(StringUtil.notBlank(fileMd5)){
			try{
				uploadPath = PropertiesUtil.getValue("upload.properties", "partyConstruction.courseware.path");
				ur = FileUploadUtil.mergeChunks(request, uploadPath,fileMd5,fileName);
				if(ur.isSuccess()){
					UploadFileInfo fileInfo = new UploadFileInfo(UUID.getUUID(), fileName, fileMd5, DateUtil.getTime(), ur.getFileType(), ur.getFielName(), ur.getRealPath(), ur.getUploadpath(), ur.getFileSize(), ur.getMediaTime());
					iUploadFileInfoService.add(fileInfo);
					ur.setRealPath(null);
				}
				logger.debug("==========ur=========="+ur);
				logger.debug("==========ur=========="+ur.getMediaTime());
			}catch(Exception e){
				logger.debug("==========上传异常==========");
			}
		}
		return ur;
	}
}
